﻿
$.fn.jqGridRowValue = function () {
    var $grid = $(this);
    var selectedRowIds = $grid.jqGrid("getGridParam", "selarrrow");
    if (selectedRowIds) {
        var json = [];
        var len = selectedRowIds.length;
        for (var i = 0; i < len; i++) {
            var rowData = $grid.jqGrid('getRowData', selectedRowIds[i]);
            json.push(rowData);
        }
        return json;
    } else {
        if (!$grid.jqGrid('getGridParam', 'multiselect'))
            return $grid.jqGrid('getRowData', $grid.jqGrid('getGridParam', 'selrow'));
        else
            return new Array();
    }
}

var adminList = {
    dom: {
        panelSearch: $('#panelSearch'),
        btnAdd: $('*[name=add]'),
        btnEdit: $('*[name=edit]'),
        btnDelete: $('*[name=delete]'),
        findBack: admin.getQueryString("findback"),
    },
    jqGridObj: null,
    //表格对象
    jqGrid: function (options) {
        $.jgrid.defaults.styleUI = "Bootstrap";
        var defaults = {
            id: "#dataTable",
            url: "",
            mtype: "post",
            datatype: "json",
            height: "calc(100vh - 160px)",
            width: ($(".jqGrid_wrapper").width()),
            multiselect: true,
            pager: "#dataTable_pager",
            rowNum: 20,
            rowList: [20, 50, 100, 1000, 2000],
            rownumbers: true,
            multiboxonly: true,
            viewrecords: true,
            reloadAfterSubmit: true,//提交完重新加载
            shrinkToFit: true,
            autoScroll: true,
            //postData: null,
            sortname: "",
            sortorder: "",
            colNames: [],
            colModel: [],
            jsonReader: null,
            onSelectRow: function (rowid, e) {
                adminList.btnControl();
            },
            ondblClickRow: function (rowid, iRow, iCol, e) {
                var rows = adminList.selectRows();
                if (adminList.dom.findBack) {
                    admin.findBack.close(rows);
                }
            },
            onSelectAll: function (rowids, status) {
                adminList.btnControl();
            },
            gridComplete: function () {
                //隐藏grid底部滚动条
                $(this).closest(".ui-jqgrid-bdiv").css({ "overflow-x": "hidden" });
            },
        };
        var options = $.extend({}, defaults, options);
        var jsonConfig = {
            url: options.url,
            mtype: options.mtype,
            datatype: options.datatype,
            height: options.height,
            width: options.width,
            multiselect: options.multiselect,
            pager: options.pager,
            rowNum: options.rowNum,
            rowList: options.rowList,
            rownumbers: options.rownumbers,
            multiboxonly: options.multiboxonly,
            viewrecords: options.viewrecords,
            reloadAfterSubmit: options.reloadAfterSubmit,
            shrinkToFit: options.shrinkToFit,
            autoScroll: options.autoScroll,
            //postData: options.postData,
            sortname: options.sortname,
            sortorder: options.sortorder,
            colNames: options.colNames,
            colModel: options.colModel,
            jsonReader: options.jsonReader,
            ondblClickRow: options.ondblClickRow,
            onSelectRow: options.onSelectRow,
            onSelectAll: options.onSelectAll,
            gridComplete: options.gridComplete,
        }

        if (options.postData != null) {
            jsonConfig.postData = options.postData;
        }

        adminList.jqGridObj = $(options.id).jqGrid(jsonConfig);

        // Add responsive to jqGrid  表格自适应
        $(window).bind('resize', function () {
            var width = $('.jqGrid_wrapper').width();
            adminList.jqGridObj.setGridWidth(width);
        });

        return adminList.jqGridObj;

    },
    //得到 用户选中得行
    selectRows: function () {
        return adminList.jqGridObj.jqGridRowValue();
    },
    //按钮控制
    btnControl: function () {
        var rows = adminList.selectRows();
        if (rows.length > 0) {

            if (rows.length == 1) {
                adminList.dom.btnEdit.removeAttr('disabled');
                adminList.dom.btnDelete.removeAttr('disabled');
            } else {
                adminList.dom.btnEdit.attr('disabled', true);
                adminList.dom.btnDelete.removeAttr('disabled');
            }

        } else {
            adminList.dom.btnEdit.attr('disabled', true);
            adminList.dom.btnDelete.attr('disabled', true);
        }
    },
    //查询面板
    panelSearch: function () {
        adminList.dom.panelSearch.toggle("fast");
    },
    //检索
    search: function () {
        adminList.refresh(null, true);
    },
    //重置检索信息
    resetSearch: function () {
        adminList.dom.panelSearch.find("form")[0].reset();
        adminList.refresh(null, true);
    },
    //刷新
    refresh: function (data, isSearch) {
        if (data || isSearch) {
            var form = adminList.dom.panelSearch.find('form');
            var postdata = adminList.jqGridObj.jqGrid('getGridParam').postData || [];

            var datas = (form.serialize()).split("&");
            for (var i = 0; i < datas.length; i++) {
                var key_value = datas[i].split("=");
                postdata[key_value[0]] = key_value[1] ? decodeURI(key_value[1]) : "";
            }

            if (data) {
                for (d in data) {
                    postdata[d] = data[d];
                }
            }

            adminList.jqGridObj.jqGrid('setGridParam', {
                postData: postdata,
                datatype: "json",
                page: 1,
            }).trigger('reloadGrid', { fromServer: true, page: 1 });
        }
        else {
            adminList.jqGridObj.trigger("reloadGrid", { fromServer: true, page: 1 });
        }
    },
    //打开 表单页
    form: function (options) {
        options.success = function (layero) {
            top.$(layero).find("iframe").attr("data-parentiframename", options.parentIframeName);
        }
        admin.openWindow(options);
    },
    //删除数据
    delete: function (url, callBack) {
        var rows = adminList.selectRows();
        if (rows.length == 0) {
            return admin.msg("请选择要移除的数据");
        }
        admin.confirm("确认删除?", function (index) {
            var json = [];
            for (var i = 0; i < rows.length; i++) {
                json.push(rows[i]._ukid);
            }
            admin.ajax({
                url: url,
                data: { ID: JSON.stringify(json) },
                success: function (r) {
                    if (r.status == 1) {
                        if (callBack) {
                            callBack();
                        }
                        admin.msg("操作成功！");
                        admin.layer.close(index);
                    }
                }
            });
        }, function () {

        });
    },
    //导出excel
    exportExcel: function (url, data) {
        $(event.srcElement).attr("href", url + "?" + adminList.dom.panelSearch.find('form').serialize() + (data ? data : ""));
    },
    //打印
    print: function (url, data) {
        $(event.srcElement).attr("href", url + "?" + adminList.dom.panelSearch.find('form').serialize() + (data ? data : ""));
    }

}
